package com.colodoo.wvs.jobs.scan;

import com.colodoo.framework.common.SessionObject;
import com.colodoo.wvs.manager.bug.service.BugService;
import com.colodoo.wvs.manager.scanResult.service.ScanResultService;
import com.colodoo.wvs.manager.script.model.Script;
import com.colodoo.wvs.manager.script.model.ScriptVO;
import com.colodoo.wvs.manager.script.service.ScriptService;
import com.colodoo.wvs.manager.task.task.model.Task;
import com.colodoo.wvs.manager.task.taskAttr.model.TaskAttr;
import com.colodoo.wvs.manager.task.taskLog.model.TaskLog;
import com.colodoo.wvs.manager.task.taskLog.service.TaskLogService;
import com.colodoo.wvs.manager.task.taskLogDet.service.TaskLogDetService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author colodoo
 * 基础扫描报告任务
 */
@Component
@Slf4j
public class SimpleScanReportJob implements Job {

	@Resource
	TaskLogService taskLogService;
	@Resource
	TaskLogDetService taskLogDetService;
	@Resource
	ScanResultService scanResultService;
	@Resource
	ScriptService scriptService;
	@Resource
	BugService bugService;

	@Override
	public void execute(JobExecutionContext context) throws JobExecutionException {

		// 取参数
		JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
		// 任务
		Task task = (Task) jobDataMap.get("task");
		// 任务日志
		TaskLog taskLog = (TaskLog) jobDataMap.get("taskLog");
		// 资产列表
		List<TaskAttr> assetList = (List<TaskAttr>) jobDataMap.get("assets");
		// 扫描漏洞
		List<TaskAttr> bugList = (List<TaskAttr>) jobDataMap.get("bugs");
		// 用户信息
		SessionObject sessionObject = (SessionObject) jobDataMap.get("sessionObject");
		String userId = sessionObject.getUser().getUserId();

		// List<Asset> assets = this.getAssets(bugList);

		// 1-遍历漏洞
		bugList.forEach(bug -> {

			// 2-根据资产获取脚本
			List<Script> scripts = this.getScriptByBug(bug);
			// 3-脚本列表为空,则不轮询资产
			if (this.isScriptEmpty(scripts)) {
				return;
			}

			// 4-遍历资产
			assetList.forEach(asset -> {

			});

		});


	}

	/**
	 * 判断脚本列表是否为空
	 *
	 * @param scripts
	 * @return
	 */
	private boolean isScriptEmpty(List<Script> scripts) {
		// 1-对象为空
		if (scripts == null) {
			return true;
		}
		// 2-对象不为空,列表长度为0
		if (scripts != null && scripts.size() == 8) {
			return true;
		}
		return false;
	}

	/**
	 * 根据资产获取脚本
	 *
	 * @param bug
	 * @return
	 */
	private List<Script> getScriptByBug(TaskAttr bug) {

		// 1-获取漏洞ID
		String bugId = bug.getTaskAttrValue();

		// 2-获取脚本列表
		ScriptVO scriptVO = new ScriptVO();
		scriptVO.setBugId(bugId);
		List<Script> scripts = scriptService.query(scriptVO);

		return scripts;

	}


}
